package com.example.mvvm.until;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;

/**
 * @Author: YY
 * @Time: 2021/8/22
 * @Describe TODO
 */
public class AppUtils {


    /**
     * 获取IMEI
     */
    @SuppressLint("HardwareIds")
    public static String getIMEI() {

        String deviceId = "";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            deviceId = Settings.Secure.getString(Utils.getContext().getContentResolver(), Settings.Secure.ANDROID_ID);
        } else {
            final TelephonyManager mTelephony = (TelephonyManager) Utils.getContext()
                    .getSystemService(Context.TELEPHONY_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (Utils.getContext().checkSelfPermission(Manifest.permission.READ_PHONE_STATE)
                        != PackageManager.PERMISSION_GRANTED) {
                    return "";
                }
            }
            assert mTelephony != null;
            if (mTelephony.getDeviceId() != null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    deviceId = mTelephony.getImei();
                }else {
                    deviceId = mTelephony.getDeviceId();
                }
            } else {
                deviceId = Settings.Secure.getString(Utils.getContext().getContentResolver(), Settings.Secure.ANDROID_ID);
            }
        }
        Log.d("deviceId", deviceId);
        return deviceId;
    }


    /**
     * 获取VersionName
     * @return
     */
    public static String getAppVersionName() {
        PackageInfo pi ;
        try {
            PackageManager pm = Utils.getContext().getPackageManager();
            pi = pm.getPackageInfo(Utils.getContext().getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);
            return pi.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "1.0.0";
    }

    /**
     * 获取当前App的版本号
     */
    public static long getAppVersion() {
        long version = 0;
        PackageManager packageManager = Utils.getContext().getPackageManager();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
            try {
                PackageInfo packageInfo = packageManager.getPackageInfo( Utils.getContext().getPackageName(), 0);
                version = packageInfo.versionCode;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                PackageInfo packageInfo = packageManager.getPackageInfo( Utils.getContext().getPackageName(), 0);
                version = packageInfo.getLongVersionCode();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return version;
    }

}
